package com.duoduo.springboot.shiro.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.duoduo.springboot.shiro.model.Permission;

@Repository
public class PermissionDao {

	private static Map<Integer, Permission> permissionDatas = new HashMap<Integer, Permission>(0);
	private static Map<Integer, List<Integer>> rolePermissionMap = new HashMap<Integer, List<Integer>>(0);

	private Map<Integer, Permission> listPermissions() {
		if (CollectionUtils.isEmpty(permissionDatas)) {
			permissionDatas.put(1, new Permission(1, "add"));
			permissionDatas.put(2, new Permission(2, "delete"));
			permissionDatas.put(3, new Permission(3, "update"));
			permissionDatas.put(4, new Permission(4, "query"));

			permissionDatas.put(5, new Permission(5, "user:query"));
			permissionDatas.put(6, new Permission(6, "user:edit"));
		}
		return permissionDatas;
	}

	private Map<Integer, List<Integer>> listRolePermission() {
		if (CollectionUtils.isEmpty(rolePermissionMap)) {
			List<Integer> permissions = new ArrayList<Integer>(0);
			permissions.add(1);
			permissions.add(2);
			permissions.add(3);
			permissions.add(4);
			permissions.add(5);
			permissions.add(6);
			rolePermissionMap.put(1, permissions);

			permissions = new ArrayList<Integer>(0);
			permissions.add(5);
			permissions.add(6);
			rolePermissionMap.put(2, permissions);
		}
		return rolePermissionMap;
	}

	public List<Permission> listPermissionsByRoleId(int roleId) {
		List<Permission> result = new ArrayList<Permission>(0);
		if (listRolePermission().containsKey(roleId)) {
			List<Integer> ids = rolePermissionMap.get(roleId);
			for (Integer id : ids) {
				result.add(listPermissions().get(id));
			}
		}
		return result;
	}
}
